﻿using CPPEI.Coolzon.UI.XProperty;
using CPPEI.Coolzon.UI.XProperty.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace CPPEI.Coolzon.UI.DataPartLib.XProperty.Converters
{
    public class XDataRangesConverter : IPropertyValueConverter
    {
        public object Convert(ConvertContext context, Metadata.ValueNode valueNode)
        {
            List<DataRange> dataRanges = new List<DataRange>();
            foreach (Metadata.ValueNode vN in valueNode.ValueNodes)
            {
                DataRange dataRange = new DataRange();
                dataRange.StartValue = double.Parse(vN.Attributes["StartValue"]);
                dataRange.EndValue = double.Parse(vN.Attributes["EndValue"]);
                dataRange.StartWidth = double.Parse(vN.Attributes["StartWidth"]);
                dataRange.EndWidth = double.Parse(vN.Attributes["EndWidth"]);
                XBrushConverter xBrushConverter = new XBrushConverter();
                foreach (Metadata.ValueNode vNChild in vN.ValueNodes)
                {
                    if (vNChild.Name.Equals("DataRange.Background"))
                    {
                        BrushEx brush = xBrushConverter.Convert(context, vNChild.ValueNodes[0]) as BrushEx;
                        dataRange.Background = brush;
                    }
                    else if (vNChild.Name.Equals("DataRange.BorderBrush"))
                    {
                        BrushEx brush = xBrushConverter.Convert(context, vNChild.ValueNodes[0]) as BrushEx;
                        dataRange.BorderBrush = brush;
                    }
                    else if (vNChild.Name.Equals("DataRange.BorderThickness"))
                    {
                        XBorderThicknessConverter xBorderThicknessConverter = new XBorderThicknessConverter();
                        Thickness thickness = (Thickness)xBorderThicknessConverter.Convert(context, vNChild.ValueNodes[0]);
                        dataRange.BorderThickness = thickness;
                    }
                }
                dataRanges.Add(dataRange);
            }
            return dataRanges;
        }

        public Metadata.ValueNode ConvertBack(ConvertContext context, object value)
        {
            List<DataRange> dataRanges = (List<DataRange>)value;
            Metadata.ValueNode valueNode = new Metadata.ValueNode("DataRanges");
            valueNode.ValueNodes = new List<Metadata.ValueNode>();

            XBrushConverter xBrushConverter = new XBrushConverter();
            XBorderThicknessConverter xBorderThicknessConverter = new XBorderThicknessConverter();
            foreach (DataRange dr in dataRanges)
            {
                Metadata.ValueNode vN = new Metadata.ValueNode("DataRange");
                vN.Attributes = new Dictionary<string, string>();
                vN.ValueNodes = new List<Metadata.ValueNode>();
                vN.Attributes.Add("StartValue", dr.StartValue.ToString());
                vN.Attributes.Add("EndValue", dr.EndValue.ToString());
                vN.Attributes.Add("StartWidth", dr.StartWidth.ToString());
                vN.Attributes.Add("EndWidth", dr.EndWidth.ToString());
                if (dr.Background != null)
                {
                    Metadata.ValueNode vNChild = new Metadata.ValueNode("DataRange.Background");
                    vNChild.ValueNodes = new List<Metadata.ValueNode>();
                    vNChild.ValueNodes.Add(xBrushConverter.ConvertBack(context, dr.Background));
                    vN.ValueNodes.Add(vNChild);
                }
                if (dr.BorderBrush != null)
                {
                    Metadata.ValueNode vNChild = new Metadata.ValueNode("DataRange.BorderBrush");
                    vNChild.ValueNodes = new List<Metadata.ValueNode>();
                    vNChild.ValueNodes.Add(xBrushConverter.ConvertBack(context, dr.BorderBrush));
                    vN.ValueNodes.Add(vNChild);
                }
                if (dr.BorderThickness != null)
                {
                    Metadata.ValueNode vNChild = new Metadata.ValueNode("DataRange.BorderThickness");
                    vNChild.ValueNodes = new List<Metadata.ValueNode>();
                    vNChild.ValueNodes.Add(xBorderThicknessConverter.ConvertBack(context, dr.BorderThickness));
                    vN.ValueNodes.Add(vNChild);
                }
                valueNode.ValueNodes.Add(vN);
            }
            return valueNode;

        }
    }
}
